package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.search.ItemVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;


import java.util.Map;
import java.util.concurrent.*;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private AlbumFeignClient albumFeignClient;
    @Autowired
    private UserFeignClient userFeignClient;
    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    private Executor executor;




//    //根据专辑ID汇总详情页所需参数
//    @Override
//    public ItemVo getItem(Long albumId) {
//        //创建ItemVo对象
//        ItemVo itemVo = new ItemVo();
//        //远程调用专辑服务: 根据专辑ID查询专辑信息
//        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
//            log.info("获取专辑信息异步任务:{}", albumId);
//            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
//            Assert.notNull(albumInfo, "专辑{}不存在", albumId);
//            itemVo.setAlbumInfo(albumInfo);
//            return albumInfo;
//        }, executor);
//        //远程调用专辑服务: 根据专辑所属分类ID查询分类信息
//        CompletableFuture<Void> baseCategoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
//
//            BaseCategoryView categoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
//            Assert.notNull(categoryView, "专辑分类{}不存在", albumInfo.getCategory3Id());
//            itemVo.setBaseCategoryView(categoryView);
//        }, executor);
//
//        //远程调用用户服务: 根据专辑所属主播ID查询主播信息
//        CompletableFuture<Void> userInfoVoCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
//            UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
//            Assert.notNull(userInfoVo, "用户{}不存在", albumInfo.getUserId());
//            itemVo.setAnnouncer(userInfoVo);
//        }, executor);
//
//        //远程调用专辑服务: 根据专辑ID查询专辑统计信息
//        CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
//            log.info("获取专辑统计异步任务:{}", albumId);
//            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
//            Assert.notNull(albumStatVo, "专辑{}信息不存在", albumId);
//            itemVo.setAlbumStatVo(albumStatVo);
//        },executor);
//        CompletableFuture.allOf(
//                albumInfoCompletableFuture,
//                baseCategoryCompletableFuture,
//                userInfoVoCompletableFuture,
//                albumStatVoCompletableFuture).orTimeout(1, TimeUnit.SECONDS).join();
//        //响应ItemVo对象
//        return itemVo;
//    }
@Override
public Map<String, Object> getItem(Long albumId) {
    //0.TODO 采用布隆过滤器解决缓存穿透问题

    //1.创建Map对象用于封装详情页数据
    Map<String, Object> map = new ConcurrentHashMap<>();

    //2.远程调用专辑服务：根据专辑ID查询专辑信息
    CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
        log.info("获取专辑信息异步任务:{}", albumId);
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
        Assert.notNull(albumInfo, "专辑{}不存在", albumId);
        map.put("albumInfo", albumInfo);
        return albumInfo;
    }, executor);

    //3.远程调用专辑服务：根据专辑所属分类ID查询分类信息
    CompletableFuture<Void> baseCategoryCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
        BaseCategoryView categoryView = albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
        Assert.notNull(categoryView, "分类{}不存在", albumInfo.getCategory3Id());
        map.put("baseCategoryView", categoryView);
    }, executor);


    //4.远程调用用户服务：根据专辑所属主播ID查询主播信息
    CompletableFuture<Void> announcerCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
        UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
        Assert.notNull(userInfoVo, "用户{}不存在", albumInfo.getUserId());
        map.put("announcer", userInfoVo);
    }, executor);

    //5.远程调用专辑服务：根据专辑ID查询专辑统计信息
    CompletableFuture<Void> albumStatVoCompletableFuture = CompletableFuture.runAsync(() -> {
        log.info("获取专辑统计异步任务:{}", albumId);
        AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
        Assert.notNull(albumStatVo, "专辑统计{}不存在", albumId);
        map.put("albumStatVo", albumStatVo);
    }, executor);


    //6.组合异步任务
    CompletableFuture.allOf(
                    albumInfoCompletableFuture,
                    baseCategoryCompletableFuture,
                    announcerCompletableFuture,
                    albumStatVoCompletableFuture
            ).orTimeout(3, TimeUnit.SECONDS)
            .join();

    //7.响应Map对象
    return map;
}
}
